import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = 'parts/api/v1';
import * as dto from './DTO';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询总代信息'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件经营权限'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑备件经营权限'
});
const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代信息'
});
const getBankDesc = formatMessage({
    id: 'api.message.getBankDesc',
    defaultMessage: '查询开户银行信息'
});
const getPartDesc = formatMessage({
    id: 'api.message.getPartDesc',
    defaultMessage: '查询备件信息'
});
const getDealerPartsDesc = formatMessage({
    id: 'api.message.exportPartDesc',
    defaultMessage: '导出总代可提报备件'
});
const detailExportDesc = formatMessage({
    id: 'api.message.detailExportDesc',
    defaultMessage: '导出可提报备件清单'
});
const importTempDesc = formatMessage({
    id: 'api.dealerOrderSubmitRulesImportTemp',
    defaultMessage: '下载导入提报规则模板'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/dealerPartInformation/init`, null, dto.init)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = bodyCondition => {
    const queryString = stringify(bodyCondition);
    return request(`${baseUrl}/dealers/forCurrentUser?${queryString}`, null, dto.list)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询总代详情 （包含备件经营权限）
export const getDetail = id => request(`${baseUrl}/dealers/${id}/forDealerPartInformation`, null, dto.detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 修改总代备件经营权限
export const updateData = (id, data) => request(`${baseUrl}/dealers/${id}/forDealerPartInformation`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询总代送达方
export const getArrivedDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forArrivedDealer?${queryString}`, null, dto.dealerSelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 查询经销商
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, dto.dealerSelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 根据国际区域查询大区
export const getMarketDepartments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketDepartments/forSubDealer?${queryString}`, null, dto.marketDepartmentList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

// 查询国际区域列表
export const getRegions = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/areaTileds/forSubDealer?${queryString}`, null, dto.regionList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
};

// 查询开户银行
export const getBanks = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/banks/forDealerPartInformation?${queryString}`, null, dto.bank)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getBankDesc));
};

// 查询备件信息
export const getParts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts?${queryString}`, null, dto.part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartDesc));
};

// 导出总代可提报备件
export const exportParts = body => request(`${baseUrl}/dealerParts/export/forDealerPartInformation?dealerId=${body}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getDealerPartsDesc)).then(res => res.data);
// 导出清单
export const detailExport = data => request(`${baseUrl}/dealerParts/export/forDealerPartInformation/details`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, detailExportDesc)).then(res => res.data);

export const dealerOrderSubmitRulesImportTemp = () => request(`${baseUrl}/dealerOrderSubmitRules/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
